package com.bbva.kyfbdaofcimpl.utils;

import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

/**
 * The Class KyfbUtils.
 */
@Component
public class KyfbUtils {
	
	/**
	 * Run sql from script resource.
	 *
	 * @param resource the resource
	 * @param jdbcTemplate the jdbc template
	 */
	@Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_UNCOMMITTED)
    public static void runSqlFromScriptResource(Resource resource, JdbcTemplate jdbcTemplate) {
        try {
            InputStream is = resource.getInputStream();
            String[] sql = IOUtils.toString(is).split(";");
             
            for (int i = 0; i < sql.length; i++) {
                jdbcTemplate.update(sql[i]);
            }
 
        } catch (Exception e) {
            throw new RuntimeException("Unable to run sql script against " + resource.getFilename(), e);
        }
    }

	
	
}
